package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.SendCode;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataZx;
import com.example.jdai.jdai.util.Tools;

import java.io.IOException;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ZxActivity extends Activity implements BackRest.DoLong {

    @BindView(R.id.tb)
    TopBar tb;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.activity_bt)
    LinearLayout activityBt;
    @BindString(R.string.no_complete)
    String onComplete;
    ProgressDialog pd;
    @BindString(R.string.sms_tip)
    String smsTip;
    @BindView(R.id.tv_noc)
    TextView tvNoc;
    @BindView(R.id.tv_noyzm)
    TextView tvNoyzm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zx);
        ButterKnife.bind(this);
        pd = new ProgressDialog(this);
        pd.setMessage(smsTip);
        pd.setCanceledOnTouchOutside(false);
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                finish();
                saveAll();
            }

            @Override
            public void more() {
                if ((!TextUtils.isEmpty(etName.getText().toString().trim())) &&
                        (!TextUtils.isEmpty(etPwd.getText().toString().trim())) &&
                        (!TextUtils.isEmpty(etCode.getText().toString().trim()))) {
                    pd.show();
                    DataAll.dataZx.data.put(DataZx.ZX_STATUS, "");
                    saveAll();
                    //ZxOk(ZxActivity.this);
                    //finish();
                    MyApplication.backRest.ZxOk(ZxActivity.this);
                    //BackRest.infoUp();
                } else {
                    Toast.makeText(ZxActivity.this, onComplete, Toast.LENGTH_SHORT).show();
                }

                //finish();

            }
        });
        initData();
    }

    void initData() {
        String name = DataAll.dataZx.data.get(DataZx.ZX_NAME);
        etName.setText(name);
        etName.setSelection(name.length());
        etPwd.setText(DataAll.dataZx.data.get(DataZx.ZX_PWD));
        etCode.setText(DataAll.dataZx.data.get(DataZx.ZX_CODE));
    }

    boolean saveAll() {
        DataAll.dataZx.data.put(DataZx.ZX_NAME, etName.getText().toString().trim());
        DataAll.dataZx.data.put(DataZx.ZX_PWD, etPwd.getText().toString().trim());
        DataAll.dataZx.data.put(DataZx.ZX_CODE, etCode.getText().toString().trim());
        return DataAll.dataZx.saveAll();
    }

    @Override
    protected void onDestroy() {
        saveAll();
        finish();
        super.onDestroy();

    }

    @Override
    protected void onPause() {
        super.onPause();
        saveAll();
    }
//
//    public void ZxOk(final Activity activity) {
//        SendCode send = new SendCode();
//        send.data.put("user_id", MyApplication.dataAll.data.get(DataAll.LOGIN_ID));
//        send.data.put("zx_username", DataAll.dataZx.data.get(DataZx.ZX_NAME));
//        send.data.put("zx_pwd", DataAll.dataZx.data.get(DataZx.ZX_PWD));
//        send.data.put("zx_code", DataAll.dataZx.data.get(DataZx.ZX_CODE));
//        Call call = MyApplication.okClient.newCall(Tools.getRequest(Config.ZX_UP_URL, send));
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                int code = response.code();
//                if (200 == code) {
//                    if (Config.ISDEBUG)
//                        Log.v("zx", response.body().string());
//                } else if (201 == code) {
//                    Tools.postToMain(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyApplication.applicationContext, "需要验证码", Toast.LENGTH_SHORT).show();
//                            Intent intent = new Intent();
//                            intent.putExtra("plat", 3);
//                            startActivity(new Intent(ZxActivity.this, YzmActivity.class));
//                        }
//                    });
//
//                }
//            }
//
//        });
//    }


    @Override
    public void doDone(int mark, Map str) {

        switch (mark){
            case 1:
                Toast.makeText(MyApplication.applicationContext,"征信需要验证码",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ZxActivity.this, YzmActivity.class);
                intent.putExtra("plat", 3);
                intent.putExtra("token", MyApplication.dataAll.data.get(DataAll.TMP));
                startActivity(intent);
            case 0:
                pd.dismiss();
                finish();
                break;
            case 2:
                Toast.makeText(MyApplication.applicationContext,"密码或账号错误",Toast.LENGTH_SHORT).show();
                pd.dismiss();
                break;

        }
    }

    @OnClick({R.id.tv_noc,R.id.tv_noyzm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_noc:
            Intent intent = new Intent(ZxActivity.this, WebActivity.class);
            intent.putExtra("url", Config.ZXZH);
            intent.putExtra("title", "征信账号");
            startActivity(intent);
                break;
            case R.id.tv_noyzm:
                Intent intentq = new Intent(ZxActivity.this, WebActivity.class);
                intentq.putExtra("url", Config.ZXZH);
                intentq.putExtra("title", "征信验证码");
                startActivity(intentq);
                break;
        }

    }

}
